package com.fg.cloud.common.auth;

import com.fg.cloud.common.ServiceException;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.model.BatchStatus;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;

/**
 * Description:七牛云空间工具类
 * @author around
 * @date 2017年8月10日下午2:15:19
 */
public class QiniuUtils {

	/** Access */
	private static final String ACCESSKEY = "8CoT9t9oVm9pHbjQQnrCNcruu9FgRGb0ESzqtRDS";
	
	/** Secret */
	private static final String SECRETKEY = "KSuL-2MAh3v0_xL9pzolV5n61tBkcFtdHbZwGdsZ";
	
	/** 基础图片空间 */
	public static final String CLOUD_BUCKET = "cloud";
	/** 基础空间域名 */
	public static final String CLOUD_URL = "http://cdn.yfx1688.com";
	
	/** 产品空间  */
	public static final String PRODUCT_BUCKET = "cloud-product";
	/** 产品空间域名 */
	public static final String PRODUCT_URL = "http://product.yfx1688.com";
	
	/**
	 * Description:简单token类型返回封装
	 * @param key 文件名
	 * @return String
	 * @author around
	 * @date 2017年8月14日上午9:57:29
	 */
	public static String getBaseToken(String bucket, String key) {
		Auth auth = Auth.create(ACCESSKEY, SECRETKEY);
		return auth.uploadToken(bucket, key);
	}
	
	/**
	 * Description:自定义token返回结果
	 * @param key 文件名
	 * @return
	 * @author around
	 * @date 2017年8月11日下午6:07:24
	 */
	public static String getCustomToken(String bucket, String key) {
		Auth auth = Auth.create(ACCESSKEY, SECRETKEY);
		StringMap putPolicy = new StringMap();
		putPolicy.put("returnBody", "{\"key\":\"$(key)\",\"com\":\"$(x:com)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"fsize\":$(fsize)}");
		long expireSeconds = 3600;
		return auth.uploadToken(bucket, key, expireSeconds, putPolicy);
	}
	
	/**
	 * Description:删除指定文件
	 * @param key 文件名
	 * @return boolean true:成功
	 * @author around
	 * @date 2017年8月11日下午6:21:18
	 */
	public static boolean remove(String bucket, String key) {
		//构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(Zone.zone0());

		Auth auth = Auth.create(ACCESSKEY, SECRETKEY);
		BucketManager bucketManager = new BucketManager(auth, cfg);
		try {
		    bucketManager.delete(bucket, key);
		} catch (QiniuException ex) {
		    //如果遇到异常，说明删除失败
			throw new ServiceException(ex.code(), ex.getMessage());
		}
		return true;
	}
	
	/**
	 * Description:批量删除文件
	 * @param keyList 文件名数组
	 * @return boolean
	 * @author around
	 * @date 2017年8月14日上午9:56:03
	 */
	public static boolean remove(String bucket, String[] keyList) {
		//构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(Zone.zone0());
		//...其他参数参考类注释

		Auth auth = Auth.create(ACCESSKEY, SECRETKEY);
		BucketManager bucketManager = new BucketManager(auth, cfg);
		try {
		    //单次批量请求的文件数量不得超过1000
		    //String[] keyList = new String[]{"qiniu.jpg","qiniu.mp4","qiniu.png",};
			BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
		    batchOperations.addDeleteOp(bucket, keyList);
		    Response response = bucketManager.batch(batchOperations);
		    BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);

		    for (int i = 0; i < keyList.length; i++) {
		        BatchStatus status = batchStatusList[i];
		        String key = keyList[i];
		        System.out.print(key + "\t");
		        if (status.code == 200) {
		            System.out.println("delete success");
		        } else {
		            System.out.println(status.data.error);
		        }
		    }
		} catch (QiniuException ex) {
			throw new ServiceException(ex.code(), ex.getMessage());
		}
		return true;
	}
	
	/**
	 * Description:查询或校验文件是否存在
	 * @param bucket
	 * @param key
	 * @return
	 * @author around
	 * @date 2017年8月30日下午2:48:01
	 */
	public static boolean select(String bucket, String key) {
		//构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(Zone.zone0());
		//...其他参数参考类注释
		Auth auth = Auth.create(ACCESSKEY, SECRETKEY);
		BucketManager bucketManager = new BucketManager(auth, cfg);
		try {
		    FileInfo fileInfo = bucketManager.stat(bucket, key);
		    System.out.println(fileInfo.hash);
		    System.out.println(fileInfo.fsize);
		    System.out.println(fileInfo.mimeType);
		    System.out.println(fileInfo.putTime);
		} catch (QiniuException ex) {
			throw new ServiceException(ex.code(), ex.getMessage());
		}
		return true;
	}
	
	
	public static void main(String[] args) {
		System.out.println(getCustomToken("cloud","test.jpg"));
		//remove("claim_guarantee.docx");
	}
	
}
